<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start(); //we need to call PHP's session object to access it through CI
class Permission extends CI_Controller {


    function __construct()
    {
        parent::__construct();
        $this->load->model('authentication','',TRUE);

    }

    function submit(){
        if($_SERVER['REQUEST_METHOD'] === 'POST'){
            $input = $this->input->post(NULL, true);
            $data = array(
                'state' => 0,/* mark as draft state */
                'toolID' => $input['tool_id'],
                'name' => $input['name'],
                'brand' => $input['brand'],
                'serialNo' => $input['serial_no'],
                'description' => $input['desc'],
                'location' => $input['location'],
                'date_received' => $input['date_received'],
                'budget_year' => $input['budget_year'],
                'unit' => $input['unit']
            );
            //$file = array(
            //    'tools_id' => $data['id'],
            //    'picture_path' => $input['picture']
            //);
            $this->db->insert('tools', $data);
            //$this->db->insert('tool_picture', $file);
            redirect('permission?action=3');
        } else {
            redirect('permission');
        }
    }
    function confirm_submit(){
        if($_SERVER['REQUEST_METHOD'] === 'POST'){
            $input = $this->input->post(NULL, true);
            $data = array(
                'state' => 1,/* mark as draft state */
            );
            $this->db->update('tools', $data);
            redirect('permission?action=3');
            ?>
        <?php
        } else {
            redirect('permission');
        }
    }
    function edit_detail(){
        if($_SERVER['REQUEST_METHOD'] === 'POST')
        {

        }
    }

    function index()
    {
        $this->load->model('tools');
        $header = array();
        $data = array();

        if($this->session->userdata('logged_in'))
        { $session_data = $this->session->userdata('logged_in');
            //$data['status'] = $session_data['status'];
            $header['username'] = $session_data['username'];
            $header['firstname'] = $session_data['firstname'];
            $header['lastname'] = $session_data['lastname'];

            $result = $this->authentication->check_user($header['username']);

            if($result)
            {
               foreach($result as $row)
                { $data['priority']= $row->priority_id;}

                if($data['priority']== 4 ){

                    if(isset($_GET['action']) && $_GET['action'] == '1'){
                        $header['tab'] = '#1';
                    }
                    if(isset($_GET['action']) && $_GET['action'] == '2'){
                        $header['tab'] = '#2';
                    }
                    if(isset($_GET['action']) && $_GET['action'] == '3'){
                        $header['tab'] = '#3';
                    }
                    if(isset($_GET['action']) && $_GET['action'] == '4'){
                        $header['tab'] = '#4';
                    }
                    if(isset($_GET['action']) && $_GET['action'] == '5'){
                        $header['tab'] = '#5';
                    }
                    if(isset($_GET['action']) && $_GET['action'] == '6'){
                        $header['tab'] = '#6';
                    }
                    if(isset($_GET['action']) && $_GET['action'] == '7'){
                        $header['tab'] = '#7';
                    }
                    if(isset($_GET['action']) && $_GET['action'] == '8'){
                        $header['tab'] = '#8';
                    }
                    $data['records'] = $this->tools->get_records(1,10);

                    $this->load->view('header', $header);
                    $this->load->view('sidebar_admin');
                    $this->load->view('index_admin', $data);
                    $this->load->view('footer');

                }elseif($data['priority']== 5){

                    $this->load->view('header', $data);
                    $this->load->view('sidebar_user');
                    $this->load->view('index_user');
                    $this->load->view('footer');

                }elseif($data['priority']== 1 || $data['priority']==  2 || $data['priority']==  3){


                    $this->load->view('header', $data);
                    $this->load->view('sidebar_supervisor');
                    $this->load->view('index_supervisor');
                    $this->load->view('footer');

                }else
                {
                    //If no session, redirect to login page
                    redirect('login', 'refresh');
                }

            }else{
                $this->load->view('header', $data);
                $this->load->view('sidebar_user');
                $this->load->view('add_user', $data);
                $this->load->view('footer');
            }




        }else {
            //If no session, redirect to login page
            redirect('login', 'refresh');
        }
    }

    function logout()
    {
        $this->session->unset_userdata('logged_in');
        session_destroy();
        redirect('home', 'refresh');
    }

}

?>